Page({
  data: {
    favorites: []
  },

  onShow() {
    // 页面显示时加载收藏列表
    this.loadFavorites();
  },

  loadFavorites() {
    const favorites = wx.getStorageSync('aiStoryFavorites') || [];
    this.setData({
      favorites: favorites
    });
  },

  viewStory(e) {
    const index = e.currentTarget.dataset.index;
    const story = this.data.favorites[index];
    // 这里可以跳转到故事详情页，或者弹窗显示完整故事
    wx.showModal({
      title: story.title || 'AI生成故事',
      content: story.content,
      showCancel: false,
      confirmText: '我知道了'
    });
  },

  deleteFavorite(e) {
    const index = e.currentTarget.dataset.index;
    let favorites = this.data.favorites;
    favorites.splice(index, 1);
    wx.setStorageSync('aiStoryFavorites', favorites);
    this.setData({
      favorites: favorites
    });
    wx.showToast({
      title: '已取消收藏',
      icon: 'success'
    });
  }
});
